package cn.stylefeng.guns.modular.bulletin.service;


import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.exceptions.BusinessException;
import cn.stylefeng.guns.modular.base.util.PageUtils;
import cn.stylefeng.guns.modular.base.util.Result;
import cn.stylefeng.guns.modular.bulletin.entity.Subscribe;
import cn.stylefeng.guns.modular.bulletin.entity.SubscribeRecords;
import cn.stylefeng.guns.modular.bulletin.mapper.SubscribeMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SubscribeService extends ServiceImpl<SubscribeMapper, Subscribe> {

    @Autowired
    private SubscribeRecordsService subscribeRecordsService;

    public Result getList() {

        Subscribe carousel=new Subscribe();
        carousel.setStatus("Y");
        List<Subscribe> list = this.list(new QueryWrapper<>(carousel));
        List< Map<String, Object>> dtoList=new ArrayList<>();

        if (list.size() > 0) {
            for(Subscribe entity:list)
            {
                Map<String, Object> map = new HashMap<>();
                dtoList.add(map);
            }
        }
        return Result.success(new PageUtils(dtoList.size(),dtoList));
    }

    public Page<Map<String, Object>> selectByCondition(String condition) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.selectByCondition(page, condition);
    }

    @Transactional
    public Boolean online(Long id){
        Subscribe subscribe = this.getById(id);
        if(subscribe.getStatus().equals("N")){
            throw new BusinessException("该币种已完成申购");
        }

        subscribe.setStatus("N");
        subscribe.setRemark("完成申购");
        this.updateById(subscribe);


        String coinName = subscribe.getCoinName();
        if(StrUtil.isEmpty(coinName)){
            throw new BusinessException("币种不能为空");
        }

        String[] split = coinName.split("/");
        if(split.length != 2){
            throw new BusinessException("币种名称需要包含/USDT");
        }



        List<SubscribeRecords> waitOnLineSubscribeRecordsList = subscribeRecordsService.getWaitOnLineSubscribeRecordsList(coinName);
        for (SubscribeRecords subscribeRecords : waitOnLineSubscribeRecordsList) {
              Boolean ok =  subscribeRecordsService.onLine(subscribeRecords,subscribe.getIssuePrice());
              if(!ok){
                  throw new BusinessException("用户id"+subscribeRecords.getUid() +"完成申购错误");
              }
        }

        return true;
    }


}
